package com.qding.park.manager.web.container;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.session.Session;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.util.Assert;

public class MyHeaderHttpSessionStrategy implements HttpSessionStrategy {

    private String tokenName = "token";

    public String getRequestedSessionId(HttpServletRequest request) {
        String token = request.getHeader(this.tokenName) == null ? request.getParameter("token")
                : request.getHeader(this.tokenName);
        return token;
    }

    public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader(this.tokenName, session.getId());
    }

    public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader(this.tokenName, "");
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }

}
